package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.CheckItemsService;

import java.util.List;

@Transactional
@Service(interfaceClass =CheckItemsService.class )
public class CheckItemsServiceImpl implements CheckItemsService{

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 新增检查项目
     * @param checkItem
     * @return
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findItemsByCondition(Integer currentPage, Integer pageSize, String queryString) {

        //第二种，Mapper接口方式的调用，推荐这种使用方式。
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page=checkItemDao.findItemsByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除
    @Override
    public void deleteItemsById(int id) {
        //查询当前检查项是否和检查组关联
        int count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            //说明有关联,不能删除
            throw new RuntimeException("检查项被检查组关联，无法删除");

        }else {
            //可以删除
            checkItemDao.deleteItemsById(id);
        }
    }

    @Override
    public CheckItem findItemsById(int id) {

        CheckItem checkItem=checkItemDao.findItemsById(id);
        return checkItem;
    }

    @Override
    public void updateItems(CheckItem checkItem) {
        checkItemDao.updateItems(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        ;
        return checkItemDao.findAll();
    }



}
